﻿using JT808Protocol.Models;
using JT808Protocol.Models.BodyParserStrategy;
using JT808Protocol.Models.BodyParserStrategy.CommonResponse;
using JT808Protocol.Models.BodyParserStrategy.Location;
using JT808Protocol.Models.BodyParserStrategy.Location.LocationAddition;
using JT808Protocol.Models.BodyStrategy.Authenticate;

namespace JT808Gateway.WebHost
{
    public static class PackageBodyExtension
    {
        public static string ReadBodyString(this JT808Package package)
        {
            string str = "";
            switch (package.Header.MessageID)
            {
                case 0x0200:
                    var drLocation = (DrLocation_0x0200)package.Body;
                    str = $"[位置信息汇报]Acc:{(drLocation.DeviceStatusBits.ACCStatus ? "开" : "关")},经纬度:{drLocation.Longtitude},{drLocation.Latitude},速度:{drLocation.Speed / 10}km/h,里程:{((LaMileage_0x01)drLocation.Additions.Find(x => x.AdditionID == EnumLocationAddition.里程)).Mileage / 10}km" +
                        $"上报时间:{drLocation.Time},流水号:{package.Header.SerialNumber}";
                    break;

                case 0x0001:
                    var drComm = (DpCommon_0x0001)package.Body;
                    str = $"[设备通用应答]结果:{drComm.Result},流水号:{package.Header.SerialNumber}";
                    break;

                case 0x0002:
                    str = "[终端心跳]";
                    break;

                case 0x0003:
                    str = "[终端注销]";
                    break;

                case 0x0100:
                    var drRegister = (DrRegisterDev_0x0100)package.Body;
                    str = $"[终端注册]省域Id:{drRegister.ProvinceID},市县域Id:{drRegister.CityID},制造商Id:{drRegister.ManufacturerID},终端型号:{drRegister.DeviceModelNum}," +
                        $"车牌号:{drRegister.LicenceNumber},流水号:{package.Header.SerialNumber}";
                    break;

                case 0x0102:
                    var drAuth = (DrAuthentication_0x0102)package.Body;
                    str = $"[终端鉴权]鉴权码:{drAuth.Akey},Imei:{drAuth.Imei},协议版本号:{package.Header.ProtocolVersion},软件版本号:{drAuth.SoftVer},流水号:{package.Header.SerialNumber}";
                    break;

                case 0x0702:
                    var drDriverReport = (DrDriverReport_0x0702)package.Body;
                    str = $"[驾驶员身份信息采集上报]插入或拔出状态:{drDriverReport.Status},插入或拔出的时间:{drDriverReport.LogOnOffTime},读卡结果:{drDriverReport.ReadingResult}," +
                        $"驾驶员姓名:{drDriverReport.DriverName},从业资格证编码:{drDriverReport.QualificationCode},发证机构:{drDriverReport.NotifiedBodyName},证件有效期:{drDriverReport.UsefulLife}" +
                        $"{(package.Header.ProtocolVersion == JT808ProtocolVersion.v2019 ? ",驾驶员身份证号:" + drDriverReport.IDCardNumber : null)}";
                    break;

                default:
                    str = $"[{package.Header.MessageID}]消息体文本读取方式未实现 ";
                    break;
            }

            return str;
        }
    }
}